<template>
  <transition name="dialog-fade">
    <div class="sxk-dialog-wrapper" v-show="visible" @click.self="handleHide">
      <div class="sxk-dialog" :style="{ width: width, marginTop: top }">
        <div class="sxk-dialog-header">
          <div>
            <slot name="title">
              <span class="title">{{ title }}</span>
            </slot>
          </div>
          <div>
            <sxk-icon name="close" @click="handleHide"></sxk-icon>
          </div>
        </div>

        <div class="sxk-dialog-body">
          <slot></slot>
        </div>

        <div class="sxk-dialog-footer" v-if="$slots.footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'sxk-dialog',
  props: {
    // 标题
    title: {
      type: String,
      default: '提示'
    },
    // 宽度
    width: {
      type: String,
      default: '30%'
    },
    // 距离顶部高度
    top: {
      type: String,
      default: '15vh'
    },
    // 显示或者隐藏
    visible: {
      type: Boolean,
      default: false
    }
  },

  methods: {
    handleHide () {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="less" scoped>
.sxk-dialog-wrapper {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: auto;
  background: rgba(0, 0, 0, 0.5);
  .sxk-dialog {
    position: relative;
    margin: 15vh auto 50px;
    background-color: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    border-radius: 4px;
    width: 30%;
    .sxk-dialog-header {
      padding: 15px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-weight: bold;
    }
    .sxk-dialog-body {
      padding: 15px;
      font-size: 13px;
    }
    .sxk-dialog-footer {
      display: flex;
      justify-content: right;
    }
  }
}

.dialog-fade-enter-active {
  animation: fade 0.2s;
}
.dialog-fade-leave-active {
  animation: fade 0.2s reverse;
}

@keyframes fade {
  0% {
    opacity: 0;
    transform: translateY(-20px);
  }
  100% {
    opacity: 1;
    transform: translateY(0px);
  }
}
</style>
